package at.technikum.bic.ese.hatcam;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Diese Activity zeigt bereits geschossene Hatcam-Fotos in einer Matrix an und
 * erlaubt die Vorschau, bzw. das L�schen von Fotos
 */
public class PicturePreviewActivity extends Activity {

	public final static String ACTION_UPDATE_LIST = "at.technikum.bic.ese.hatcam.PicturePreviewActivity.ACTION_UPDATE_LIST";

	private String[] filePaths;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picturepreview);
		
		loadFilePaths();
		
		Gallery gallery = (Gallery) findViewById(R.id.picture_gallery);
		gallery.setAdapter(new ImageAdapter(this));
		
		final ImageView picture_preview = (ImageView) findViewById(R.id.picture_preview);
		
		gallery.setOnItemClickListener(new OnItemClickListener() {

			
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				Toast.makeText(getBaseContext(), filePaths[position], Toast.LENGTH_SHORT).show();
				picture_preview.setImageBitmap(loadBitmapImage(filePaths[position], 8));
				//picture_preview.invalidate();
			}

		});
	}

	private void loadFilePaths() {
		File dir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/hatcam" );    
		
        File[] files = dir.listFiles(new FilenameFilter() { 
			
			public boolean accept(File dir, String name) {
				if (new File(dir,name).isDirectory())
					return false;
				return name.toLowerCase().endsWith(".jpg");
			}
		});
        
        filePaths = new String[files.length];
        
        for(int i = 0; i < files.length; i++) {
        	filePaths[i] = files[i].getAbsolutePath();
        }
	}
	
	@Override
	protected void onResume() {
		//adapter = new ImageAdapter(this);
		
		// Setze unseren ImageAdapter als Datenquelle
		//pictureGrid.setAdapter(adapter);
		super.onResume();
	}


	private class ImageAdapter extends BaseAdapter {
	    private Context mContext;
	    
	    
	    public ImageAdapter(Context c) {
	        mContext = c;
	    }

	    public int getCount() {
	        return filePaths.length;
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(final int position, View convertView, ViewGroup parent) {
	    	ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	        
            Bitmap preview_bitmap = loadBitmapImage(filePaths[position], 32);
	        imageView.setImageBitmap(preview_bitmap);
	     
	        return imageView;
	    }
	}

	private Bitmap loadBitmapImage(String uri, int sampleSize) {
		BitmapFactory.Options options=new BitmapFactory.Options();
        
		options.inSampleSize = sampleSize;
        
        Bitmap preview_bitmap = BitmapFactory.decodeFile(uri, options);
        
        return preview_bitmap;
	}
}


